var app = getApp();
const pathUrl = app.globalData.pathUrl;
Page({
  data: {
    pathUrl: pathUrl,
    openId: '',
    proNo: '',
    orderNo: '',
    proIsdb: '', //是否有担保人1是
    proAttList: [], //上传附件列表
    array: ['新车', '二手车'],
    array1: ['3', '6', '9', '12', '18', '24', '36', '48','60'],
    array2: ['先放款后抵押', '先抵押后放款', '第三方机构管理'],
    flag: true,
    isSubmit: true, //防止重复提交
    bankCode: '0140808524',
    assurerNo: 'S14081592',
    platNo: 'qzyt',
    busiCode: '1001',
    productType: '1',
    downloadMode: '1',

  },
  onLoad: function(e) {
    var openId = wx.getStorageSync('openId');
    var proNo = e.proNo;
    var proName = e.proName;
    this.setData({
      proNo: proNo,
      openId: openId,
      proName: proName
    })
    wx.setNavigationBarTitle({
      title: proName
    })

    console.log("proNo:" + proNo)
    this.getno();
    // this.getProAttList();
  },

  addMaterial1: function(e) {
    console.log(e)
    var that = this;
    wx.navigateTo({
      url: '/pages/addMaterial/addMaterial?proNo=' + that.data.proNo + '&proName=' + e.currentTarget.dataset.tit + '&orderNo=' + e.currentTarget.dataset.orderno + '&type=zjr',
    })
  },
  goadd31: function(e) {
    console.log(e)
    var that = this;
    wx.navigateTo({
      url: '/pages/add31/add31?proNo=' + that.data.proNo + '&proName=' + e.currentTarget.dataset.tit + '&orderNo=' + e.currentTarget.dataset.orderno + '&type=dbr' + '&nameNumber=' + e.currentTarget.dataset.namenumber,
    })
  },

  bindRelationship: function(e) {
    var index = e.detail.value;
    var orderPeriod = this.data.array[index];
    this.setData({
      pickerIndex: index,
      orderPeriod: orderPeriod
    })

  },
  bindRelationship1: function(e) {
    var index = e.detail.value;
    var orderPeriod1 = this.data.array1[index];
    this.setData({
      pickerIndex1: index,
      orderPeriod1: orderPeriod1
    })
  },
  bindRelationship2: function(e) {
    var index = e.detail.value;
    var orderPeriod2 = this.data.array2[index];
    var number = null;
    if (index == '0') {
      this.setData({
        number: '5',
        pickerIndex2: index,
        orderPeriod2: orderPeriod2
      })
    }
    if (index == '1') {
      this.setData({
        number: '6',
        pickerIndex2: index,
        orderPeriod2: orderPeriod2
      })
    }
    if (index == '2') {
      this.setData({
        number: '8',
        pickerIndex2: index,
        orderPeriod2: orderPeriod2
      })
    }
  },
  getno: function() {
    var that = this;
    wx.request({
      url: pathUrl + '/rest/base/getno/' + that.data.proNo,
      success: function(res) {
        if (res.data.ok || res.data.data) {
          that.setData({
            orderNo: res.data.data
            // orderNo: 'DD20200403-0006'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取数据失败',
            confirmColor: '#7aa6d1',
            showCancel: false
          })
        }
      }
    })
  },
  bankCode: function(e) {
    this.setData({
      bankCode: e.detail.value
    })
  },
  assurerNo: function(e) {
    this.setData({
      assurerNo: e.detail.value
    })
  },
  platNo: function(e) {
    this.setData({
      platNo: e.detail.value
    })
  },
  busiCode: function(e) {
    this.setData({
      busiCode: e.detail.value
    })
  },
  productType: function(e) {
    this.setData({
      productType: e.detail.value
    })
  },
  orderNo: function(e) {
    this.setData({
      orderNo: e.detail.value
    })
  },
  downloadMode: function(e) {
    this.setData({
      downloadMode: e.detail.value
    })
  },
  intentionPrice: function(e) {
    this.setData({
      intentionPrice: e.detail.value
    })
  },
  idCard: function(e) {
    this.setData({
      idCard: e.detail.value
    })
  },
  bankCardNo: function(e) {
    this.setData({
      bankCardNo: e.detail.value
    })
  },
  signMode: function(e) {
    this.setData({
      signMode: e.detail.value
    })
  },
  issueAuthority: function(e) {
    this.setData({
      issueAuthority: e.detail.value
    })
  },
  phoneNum: function(e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },
  userName: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  startDate: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  endDate: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  familyAddress: function(e) {
    this.setData({
      familyAddress: e.detail.value
    })
  },
  loanMoney: function(e) {
    this.setData({
      loanMoney: e.detail.value
    })
  },
  repayPeriod: function(e) {
    this.setData({
      repayPeriod: e.detail.value
    })
  },
  upEnclosure: function(e) {
    var that = this;
    var sbtype = e.currentTarget.dataset.sbtype
    wx.chooseImage({
      count: 1, //最多可以选择的图片总数  
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 20000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: pathUrl + '/rest/base/uploadsb',
            filePath: tempFilePaths[i],
            name: 'uploadfile_ant',
            formData: {
              sbtype: sbtype,
              filetype: 'sb',
              orderNo: that.data.orderNo
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function(res) {
              uploadImgCount++;
              console.log(res);
              if (sbtype == 2) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;

                that.setData({
                  userName: items[1].content,
                  // ocrSex: items[2].content,
                  // ocrNation: items[3].content,
                  // ocrBirth: items[4].content,
                  familyAddress: items[5].content,
                  idCard: items[6].content,
                })
                console.log("sbtype == 2:" + JSON.stringify(items))
              } else if (sbtype == 3) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                var startDate = items[3].content;
                startDate = startDate.replace(/-/g, '.')
                var endDate = items[4].content;
                endDate = endDate.replace(/-/g, '.')

                that.setData({
                  issueAuthority: items[1].content,
                  // ocrValidityPeriod: items[2].content,
                  startDate: startDate,
                  endDate: endDate
                })
                console.log("sbtype == 3:" + JSON.stringify(items))

              } else if (sbtype == 6) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                that.setData({
                  brand: items[5].content,
                  plateNumber: items[1].content,
                  holder: items[3].content,
                  dateIssue: items[8].content,
                  nature: items[10].content,
                  address: items[4].content,
                  vinno: items[6].content,
                  engineNumber: items[7].content,
                })
                console.log("sbtype == 6:" + JSON.stringify(items))
              } else if (sbtype == 5) {
                var data = JSON.parse(res.data);
                data = JSON.parse(data[0].piccontent);
                var items = data.result.cardsinfo[0].items;
                that.setData({
                  holder1: items[2].content,
                  dateIssue1: items[6].content,
                  nature1: items[7].content,
                  address1: items[4].content,

                })
                console.log(data);
                console.log(items);
              }
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function(res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false
              })
            }
          });
        }
      }
    });
  },
  submitBtn: function() {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      });
      var data = {
        createBy: that.data.openId,
        bankCode: that.data.bankCode,
        assurerNo: that.data.assurerNo,
        platNo: that.data.platNo,
        busiCode: '1001',
        productType: that.data.productType,
        orderNo: that.data.orderNo,
        carType: that.data.pickerIndex,
        downloadMode: '1',
        intentionPrice: that.data.intentionPrice,
        businessModel: that.data.number,
        userName: that.data.userName,
        idCard: that.data.idCard,
        familyAddress: that.data.familyAddress,
        issueAuthority: that.data.issueAuthority,
        startDate: that.data.startDate,
        endDate: that.data.endDate,
        bankCardNo: that.data.bankCardNo,
        signMode: '2',
        phoneNum: that.data.phoneNum,
        loanMoney: that.data.loanMoney,
        repayPeriod: that.data.orderPeriod1,
        fxjOut01: that.data.proName,
      };
      console.log("data:" + JSON.stringify(data));
      wx.request({
        url: pathUrl + '/rest/hrzxjj01Controller' ,
        data: data,
        method: 'POST',
        success: function(res) {
          console.log(res.data)
          if (res.data.ok) {
            that.setData({
              isSubmit: true
            });
            wx.showToast({
              title: '提交成功',
              duration: 1000,
              mask: true,
              success: function() {
                setTimeout(function() {
                  wx.switchTab({
                    url: "/pages/index/index"
                  })
                }, 1000);
              }
            })
          } else {
            that.setData({
              isSubmit: true
            });
            wx.showToast({
              title: '提交失败',
              icon: 'none',
              mask: true,
              duration: 1000
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'none',
        mask: true,
        duration: 1000
      })
    }
  },

  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})